package com.xzx.study;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class HandlerDemoActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = HandlerDemoActivity.class.getSimpleName();
    private static final int WHAT_INCREASE = 1;
    private static final int WHAT_DECREASE = 2;
    private static final int WHAT_PAUSE = 3;

    private TextView handler_demo_num;
    private Button handler_demo_add;
    private Button handler_demo_sub;
    private Button handler_demo_pause;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler_demo);

        handler_demo_num = findViewById(R.id.handler_demo_num);
        handler_demo_add = findViewById(R.id.handler_demo_add);
        handler_demo_sub = findViewById(R.id.handler_demo_sub);
        handler_demo_pause = findViewById(R.id.handler_demo_pause);

        handler_demo_add.setOnClickListener(this);
        handler_demo_sub.setOnClickListener(this);
        handler_demo_pause.setOnClickListener(this);
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int num = Integer.parseInt(handler_demo_num.getText().toString());
            switch (msg.what) {
                case WHAT_INCREASE:// 收到递增的消息，移除掉递减的消息
                    handler.removeMessages(WHAT_DECREASE);

                    handler_demo_add.setEnabled(false);
                    handler_demo_sub.setEnabled(true);
                    handler_demo_pause.setEnabled(true);

                    if (num == 20) {
                        //达到上限
                        Toast.makeText(HandlerDemoActivity.this, "已经达到上限", Toast.LENGTH_SHORT).show();
                        handler_demo_add.setEnabled(false);
                        handler_demo_pause.setEnabled(false);
                        return;
                    }
                    num++;
                    handler_demo_num.setText(String.valueOf(num));
                    handler.sendEmptyMessageDelayed(WHAT_INCREASE, 1000);
                    break;
                case WHAT_DECREASE:// 收到递增的消息，移除掉递减的消息
                    handler.removeMessages(WHAT_INCREASE);

                    handler_demo_add.setEnabled(true);
                    handler_demo_sub.setEnabled(false);
                    handler_demo_pause.setEnabled(true);

                    if (num == 0) {
                        //达到下限
                        Toast.makeText(HandlerDemoActivity.this, "已经达到下限", Toast.LENGTH_SHORT).show();
                        handler_demo_sub.setEnabled(false);
                        handler_demo_pause.setEnabled(false);
                        return;
                    }

                    num--;
                    handler_demo_num.setText(String.valueOf(num));
                    handler.sendEmptyMessageDelayed(WHAT_DECREASE, 1000);
                    break;
                case WHAT_PAUSE:
                    handler.removeMessages(WHAT_INCREASE);
                    handler.removeMessages(WHAT_DECREASE);

                    handler_demo_add.setEnabled(true);
                    handler_demo_sub.setEnabled(true);
                    handler_demo_pause.setEnabled(false);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onClick(View v) {
        if (v == handler_demo_add) {
            handler.sendEmptyMessage(WHAT_INCREASE);
        } else if (v == handler_demo_sub) {
            handler.sendEmptyMessage(WHAT_DECREASE);
        } else {
            handler.sendEmptyMessage(WHAT_PAUSE);
        }
    }
}
